package fichier;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import java.io.File;
import java.sql.*;


public class ModifierModele2 extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2546027855591398278L;
	/**
	 * 
	 */
	private JPanel pane;
	private JLabel designation;
	private JLabel client;
	private JLabel box;
	private JLabel annee;
	private JLabel numero;
	private JLabel numeroPlan;
	private JLabel commentaire;
	private JLabel nbMoules;
	private JLabel nbBoites;
	//private JLabel image;
	private JLabel clientBox ;
	private JLabel anneeTxt;
	private JLabel designationTxt;
	private JLabel numeroTxt;
	private JLabel numeroPlanTxt;
	private JLabel commentaireTxt;
	private JLabel nbMoulesTxt;
	private JLabel nbBoitesTxt ;
	private JButton modifBut ;
	private JButton cancelBut ;
	private JMenuBar menuBar;
	private JMenu mModele;
	private JMenu mClient;
	private JMenu fichier;
	private JMenuItem deconnexion;
	private JMenuItem miRechercherM;
	private JMenuItem miAjouterM;
	private JMenuItem miAjouterC;
	private JMenuItem miModifierPwd;
	private JMenuItem miAjoutBox;
	public String[] clientboxtab;
	private File imageFile;
	public static final String pathImage = "/Users/yannlarriau/Documents/workspace/Model Manager/Images/";
	public int iD;
	private Modele aModifier;

	public ModifierModele2(int iD)
	{
		super("Gestion des modèles - AFP Entreprises");
		this.iD=iD;




		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setBounds(100,100,565,355 );
		this.setImageFile(new File(pathImage + "imagenonexistante.jpg"));

		main.updateModel();


		this.menuBar = new JMenuBar();
		this.mModele = new JMenu("Modèle");
		this.mClient = new JMenu("Client");
		this.fichier = new JMenu("Fichier");
		this.deconnexion = new JMenuItem("Quitter");
		this.miRechercherM = new JMenuItem("Rechercher");
		this.miAjouterM = new JMenuItem("Ajouter");
		new JMenuItem("Modifier");
		this.miAjouterC = new JMenuItem("Ajouter");
		this.miModifierPwd = new JMenuItem("Modifier mot de passe");
		this.miAjoutBox = new JMenuItem("Ajouter un nouveau Box");

		this.menuBar.add(this.fichier);
		this.menuBar.add(this.mModele);
		this.fichier.add(this.miAjoutBox);
		this.fichier.add(this.miModifierPwd);
		this.fichier.add(this.deconnexion);
		this.mModele.add(this.miRechercherM);
		if(Connexion.autorisation==1)
		{
		this.menuBar.add(this.mClient);
		this.mClient.add(this.miAjouterC);
		this.mModele.add(this.miAjouterM);
		}
		this.setJMenuBar(this.menuBar);

		this.pane = new JPanel();
		this.designation = new JLabel("Désignation :");
		this.client = new JLabel("Client :");
		this.box = new JLabel("N° Box :");
		this.annee = new JLabel("Date :");
		this.numero = new JLabel("Numéro :");
		this.numeroPlan = new JLabel("N° Plan :");
		this.commentaire = new JLabel("Commentaire :");
		this.nbMoules = new JLabel("Nombre de Modèles :");
		this.nbBoites = new JLabel("Nombre de Boites :");
		//this.image = new JLabel("Image");
		Font newLabelFont=new Font(this.designation.getFont().getName(),Font.BOLD,this.designation.getFont().getSize());

		this.designation.setFont(newLabelFont);  
		this.client.setFont(newLabelFont);  
		this.box.setFont(newLabelFont);  
		this.annee.setFont(newLabelFont);  
		this.numero.setFont(newLabelFont);  
		this.numeroPlan.setFont(newLabelFont);  
		this.commentaire.setFont(newLabelFont);  
		this.nbMoules.setFont(newLabelFont);  
		this.nbBoites.setFont(newLabelFont); 

		
		main.boxBox = new JComboBox(main.model);

		SelectData.REQUETE = "select designation_modele, " +
				"nom_client, " +
				"numero_modele, " +
				"numero_plan_modele, " +
				"annee_modele, " +
				"nom_box, " +
				"commentaire_modele, " +
				"nombre_moule_modele, " +
				"nombre_boite_modele " +
				"from modele m " +
				"join client c on m.id_client = c.id_client " +
				"join box b on m.id_box = b.id_box " +
				"where m.id_modele = " + iD;
		ResultSet infoModele = SelectData.startSelect();

		try {
			if(infoModele.next())
			{
				this.clientBox = new JLabel(infoModele.getString("nom_client"));
				main.boxBox.setSelectedItem(infoModele.getString("nom_box"));
				this.anneeTxt = new JLabel(infoModele.getString("annee_modele"));
				this.designationTxt = new JLabel(infoModele.getString("designation_modele"));
				this.numeroTxt = new JLabel(infoModele.getString("numero_modele"));
				this.numeroPlanTxt = new JLabel(infoModele.getString("numero_plan_modele"));
				this.commentaireTxt = new JLabel(infoModele.getString("commentaire_modele"));
				this.nbMoulesTxt = new JLabel(infoModele.getString("nombre_moule_modele"));
				this.nbBoitesTxt = new JLabel(infoModele.getString("nombre_boite_modele"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		this.modifBut = new JButton("Valider");
		this.cancelBut = new JButton("Annuler");
		this.pane.setBorder(BorderFactory.createTitledBorder("Modification de modèle"));


		this.getContentPane().add(this.pane);
		this.pane.add(designation);
		this.pane.add(client);
		this.pane.add(box);
		this.pane.add(annee);
		this.pane.add(numero);
		this.pane.add(numeroPlan);
		this.pane.add(commentaire);
		this.pane.add(nbMoules);
		this.pane.add(nbBoites);
		//this.pane.add(image);
		this.pane.add(designationTxt);
		this.pane.add(clientBox);
		this.pane.add(main.boxBox);
		this.pane.add(anneeTxt);
		this.pane.add(numeroTxt);
		this.pane.add(numeroPlanTxt);
		this.pane.add(commentaireTxt);
		this.pane.add(nbMoulesTxt);
		this.pane.add(nbBoitesTxt);
		this.pane.add(modifBut);
		this.pane.add(cancelBut);


		this.getContentPane().setLayout(null);
		this.pane.setLayout(null);

		this.pane.setBounds(5,5,540,285);
		this.designation.setBounds(30,20,120,20);
		this.client.setBounds(330,20,120,20);
		this.box.setBounds(330,100,120,20);
		this.annee.setBounds(30,100,120,20);
		this.numero.setBounds(30,60,120,20);
		this.numeroPlan.setBounds(330,60,120,20);
		this.commentaire.setBounds(30,140,120,20);
		this.nbMoules.setBounds(30,180,143,20);
		this.nbBoites.setBounds(290,180,130,20);
		//this.image.setBounds(30,260,120,20);
		this.designationTxt.setBounds(150,20,120,20);
		this.numeroTxt.setBounds(150,60,120,20);
		this.numeroPlanTxt.setBounds(410,60,120,20);
		main.boxBox.setBounds(410,100,120,20);
		this.anneeTxt.setBounds(150,100,120,20);
		this.clientBox.setBounds(410,20,120,20);
		this.commentaireTxt.setBounds(150,140,380,20);
		this.nbMoulesTxt.setBounds(175,180,110,20);
		this.nbBoitesTxt.setBounds(420,180,110,20);
		this.modifBut.setBounds(280,240,110,20);
		this.cancelBut.setBounds(400,240,110,20);

		this.miAjoutBox.addActionListener(this);
		this.miModifierPwd.addActionListener(this);
		this.deconnexion.addActionListener(this);
		this.modifBut.addActionListener(this);
		this.cancelBut.addActionListener(this);
		this.miAjouterC.addActionListener(this);
		this.miRechercherM.addActionListener(this);
		this.miAjouterM.addActionListener(this);


		this.setResizable(false);
		this.setVisible(true);
	}



	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == this.modifBut){


			this.aModifier = new Modele(this.iD);
			String error = new String();
			error ="";

			String boxAdd			= ""+main.boxBox.getSelectedItem();

			if(boxAdd.equals(""))
				error = error + "Pas de box\n";

			if(error.equals(""))
			{
				Client c = new Client((String)this.clientBox.getText());
				Box b = new Box((String)main.boxBox.getSelectedItem(),1);

				this.aModifier.setClient(c);
				this.aModifier.setEmplacement(b);
				this.aModifier.setDesignation(this.designationTxt.getText());
				this.aModifier.setNumero_modele(this.numeroTxt.getText());
				this.aModifier.setNumero_plan(this.numeroPlanTxt.getText());
				this.aModifier.setCommentaire(this.commentaireTxt.getText());
				this.aModifier.setNombre_moule(Integer.parseInt(this.nbMoulesTxt.getText()));
				this.aModifier.setNombre_boite(Integer.parseInt(this.nbBoitesTxt.getText()));
				this.aModifier.setAnneeCreation(this.anneeTxt.getText());

				int q = JOptionPane.showConfirmDialog(this,"Etes vous sur de vouloir modifier le modèle ?", "Attention",JOptionPane.YES_NO_OPTION);
				if(q==0){
					this.aModifier.update();
					if(main.testUpdate){
						this.dispose();
						@SuppressWarnings("unused")
						AfficherModele m = new AfficherModele(this.iD);
					}
					else
						JOptionPane.showMessageDialog(this,"Le modèle n'a pas pu être modifié pour une erreur inconnue", "Attention", JOptionPane.OK_OPTION,new ImageIcon(getClass().getResource("/icon/warn.png")));
				}
			}
			else
				JOptionPane.showMessageDialog(this,error, "Attention", JOptionPane.OK_OPTION,new ImageIcon(getClass().getResource("/icon/warn.png")));
		}
		if(e.getSource()==cancelBut)
		{
			int q = JOptionPane.showConfirmDialog(this,"Etes-vous sur de vouloir annuler la modification ?", "Annulation", JOptionPane.YES_NO_OPTION);
			if(q==0)
			{
				this.dispose();
				@SuppressWarnings("unused")
				AfficherModele a = new AfficherModele(this.iD);
			}
		}
		if(e.getSource() == this.miRechercherM){
			this.dispose();
			@SuppressWarnings("unused")
			RechercheModele m = new RechercheModele();
		}
		if(e.getSource() == this.miAjouterM){
			this.dispose();
			@SuppressWarnings("unused")
			AjouterModele maFenetre = new AjouterModele();
		}
		if(e.getSource() == this.miAjouterC){
			this.dispose();
			@SuppressWarnings("unused")
			AjouterClient maFenetre = new AjouterClient();
		}
		if(e.getSource() == this.miModifierPwd){
			@SuppressWarnings("unused")
			ChangeMdP password = new ChangeMdP(this,true);
		}
		if(e.getSource() == this.miAjoutBox){
			@SuppressWarnings("unused")
			AjoutBox box = new AjoutBox(this, true);
		}
		if(e.getSource() == this.deconnexion){
			System.exit(0);	
		}
	}



	public File getImageFile() {
		return imageFile;
	}



	public void setImageFile(File imageFile) {
		this.imageFile = imageFile;
	}


}

